#include "moveeater.h"
#include "mouseeater.h"

MoveEater::MoveEater(QWidget *parent, QWidget *target)
    : QObject{}
    , m_parent(parent)
    , m_target(target)
{
    if (m_target == nullptr) {
        m_target = parent;
    }

    new MouseEater([this](QMouseEvent *event){
        static QPoint mousePoint;
        static bool mousePressed = false;

        if (event->type() == QMouseEvent::MouseButtonPress){
            mousePressed = true;
            mousePoint = event->globalPos() - m_target->pos();
        } else if (event->type() == QMouseEvent::MouseButtonRelease) {
            mousePressed = false;
        } else if (event->type() == QMouseEvent::MouseMove) {
            if (mousePressed && (event->buttons() & Qt::LeftButton)) {
                m_target->move(event->globalPos() - mousePoint);
                return true;
            }
        }
        return false;
    }, m_parent);
}
